namespace Pap.Core.Bootstrappers
{
    using System.Configuration;
    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;
    using Microsoft.Practices.Unity.ServiceLocatorAdapter;

    /// <summary>
    /// 
    /// </summary>
    public static class Bootstrapper
    {
        /// <summary>
        /// Initializes the <see cref="Bootstrapper"/> class.
        /// </summary>
        static Bootstrapper()
        {
            ConfigureContainer();
        }

        /// <summary>
        /// Runs this instance.
        /// </summary>
        public static void Run()
        {
            var tasks = ServiceLocator.Current.GetAllInstances<IBootstrapperTask>();

            foreach (IBootstrapperTask task in tasks)
            {
                task.Execute();
            }
        }

        /// <summary>
        /// Configures the container.
        /// </summary>
        private static void ConfigureContainer()
        {
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            configuration.Containers.Default.Configure(container);
            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
        }
    }
}